package me.shiqiang.im.action;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tst.common.returnobj.BaseReturnObj;
import com.tst.common.returnobj.ReturnObj;
import com.tst.common.returnobj.ReturnObjCode;
import me.shiqiang.im.domain.Friend;
import me.shiqiang.im.domain.User;
import me.shiqiang.im.domain.UserFriends;
import me.shiqiang.im.service.UserGroupsService;
import me.shiqiang.im.service.UserService;
import me.shiqiang.im.vo.AgreeFriendRequestVo;
import me.shiqiang.im.vo.FriendRequestVo;
import me.shiqiang.im.vo.GroupPropsVo;
import me.shiqiang.im.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author cuishiqiang
 * @date 2017/12/8 下午4:01
 */
@RestController
@RequestMapping("im/users")
public class UserAction {

    @Autowired
    UserService userService;

    @Autowired
    UserGroupsService userGroupsService;


    @PutMapping("/chatSwitch/{userId}/{type}")
    public ReturnObj chatSwitch(@PathVariable Long userId, @PathVariable int type) {
        userService.chatSwitch(userId, type);
        return new ReturnObj(ReturnObjCode.SUCCESS, "变更成功");
    }

    @GetMapping("/chatSwitch/{userId}")
    public ReturnObj chatSwitch(@PathVariable Long userId) {
        boolean result = userService.chatSwitch(userId);
        return new ReturnObj(ReturnObjCode.SUCCESS, result);
    }

    @GetMapping("/chatSwitch/{userId}/forBusiness")
    public boolean chatSwitchForBusiness(@PathVariable Long userId) {
        return userService.chatSwitch(userId);
    }

    @GetMapping("{userId}")
    public ReturnObj<UserVo> get(@PathVariable Long userId) {
        User user = userService.get(userId);
        return new ReturnObj<>(ReturnObjCode.SUCCESS, UserVo.of(user));
    }

    @PostMapping("save")
    public ReturnObj save(User user) {
        Long id = userService.save(user);
        return new ReturnObj<>(ReturnObjCode.SUCCESS, id);
    }

    @PostMapping("register")
    public ReturnObj register(String phone, String pwd) {
        Long id = userService.register(phone, pwd);
        return new ReturnObj<>(ReturnObjCode.SUCCESS, id);
    }

    @PostMapping("login")
    public ReturnObj login(String userName,String pwd) {
        User user = userService.login(userName,pwd);
        return new ReturnObj<>(ReturnObjCode.SUCCESS, UserVo.of(user));
    }

    @PostMapping("modifyPwd")
    public BaseReturnObj modifyPwd(Long userId, String pwd, String newPwd) {
        userService.modifyPwd(userId, pwd, newPwd);
        return new BaseReturnObj(ReturnObjCode.SUCCESS, "密码修改成功");
    }

    @PostMapping("modifyInfo")
    public BaseReturnObj modifyInfo(Long userId, String name,String avatar, Integer gender) {
        userService.modifyInfo(userId, name, avatar, gender);
        return new BaseReturnObj(ReturnObjCode.SUCCESS, "密码修改成功");
    }

    @PutMapping("{userId}/friend/{friendId}")
    public String addFriend(@PathVariable Long userId, @PathVariable Long friendId) {
        userService.addFriend(userId, friendId);
        return "success";
    }

    @GetMapping("{id}/friendRequests")
    public ReturnObj getFriendRequestList(@PathVariable Long id) throws IOException {
        List list = userService.getFriendRequestList(id);
        return new ReturnObj(ReturnObjCode.SUCCESS, list);
    }

    @PutMapping("newFriendRequest/")
    public String newFriendRequest(@RequestBody FriendRequestVo vo) throws JsonProcessingException {
        userService.newFriendRequest(vo);
        return "success";
    }

    @PutMapping("agreeFriendRequest")
    public String agreeFriendRequest(@RequestBody AgreeFriendRequestVo vo) throws JsonProcessingException {
        userService.agreeFriendRequest(vo);
        return "success";
    }

    @DeleteMapping("{userId}/friend/{friendId}")
    public void deleteFriend(@PathVariable Long userId, @PathVariable Long friendId) {
        userService.deleteFriend(userId, friendId);
    }

    @GetMapping("{userId}/friends")
    public Collection<Friend> getFriends(@PathVariable Long userId) {
        UserFriends userFriends = userService.getUserFriends(userId);
        if (userFriends == null) {
            return Collections.emptyList();
        }
        return userFriends.getFriends().values();
    }


    @GetMapping("{userId}/groups")
    public List<GroupPropsVo> getGroups(@PathVariable Long userId) {
        return userGroupsService.getGroupList(userId);
    }


    @GetMapping("useAuth/{userId}")
    public Boolean useAuth(@PathVariable Long userId) {
        return userService.useAuth(userId);
    }

    @PutMapping("publicity/{open}")
    public void publicity(@PathVariable String open) {
        userService.publicity(open);
    }

    @PutMapping("useAuth/{userId}")
    public void addUseAuth(@PathVariable Long userId) {
        userService.addUseAuth(userId);
    }

}
